package com.study.spring.proxy;

import com.study.spring.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {
//        Hum hum = (Hum) ProxyFactory.getProxyInstance(new Man());
//
//        Object proxyInstance = ProxyFactory.getProxyInstance(new Man());
//
//        int result = hum.count(2,3);
//        System.out.println(result);
//        hum.eat("lalala");

        //Map map = new HashMap();
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //Demo bean = context.getBean(Demo.class);
        //B b = context.getBean(B.class);
        //B b = bean.b;
        //bean.add();

        DemoB demoB =context.getBean(DemoB.class);
        DemoA demoA = context.getBean(DemoA.class);

        //System.out.println(111);
    }
}


interface Hum {
    void eat(String food);

    int count(int a, int b);
}

class Man implements Hum {

    public void eat(String food) {

    }

    public int count(int a, int b) {
        return a + b;
    }

}

class MyInvokeHandle implements InvocationHandler{
    private Object obj ;

    public MyInvokeHandle(Object obj) {
        this.obj = obj;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("do some things before count");
        Object result =  method.invoke(obj, args);
        System.out.println("do some things after count");

        return result;
    }
}

class ProxyFactory{

    public static Object getProxyInstance(Object obj){

         return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(), new MyInvokeHandle(obj));
    }


}